<%--
  Created by IntelliJ IDEA.
  User: gsb
  Date: 2019/11/22
  Time: 18:02
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>试题新建编辑</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">

    <link rel="stylesheet" href="/lib/layui/layui/css/layui.css">
    <link rel="stylesheet" href="/lib/zTree_v3/css/zTreeStyle/zTreeStyle.css">
    <link rel="stylesheet" href="/lib/layui/layui/css/style.css?20200325.1">
    <link rel="stylesheet" href="/lib/layui/layui/css/common.css">
    <link rel="stylesheet" href="/lib/layui/layui/css/eleTree.css">
    <script type="text/javascript" src="/js/jquery/jquery-1.9.1.js"></script>
    <script src="/lib/jquery.form.min.js"></script>
    <script src="/lib/layui/layui/layui.js"></script>
    <script src="/lib/layui/layui/js/common.js" type="text/javascript" charset="utf-8"></script>
    <script src="/lib/layui/layui/js/baseCode/base.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript" src="/lib/layui/layui/global.js?20191126"></script>
    <script src="/js/base/base.js" type="text/javascript" charset="utf-8"></script>

    <style>
        .layui-form-label{width:113px;}
        .layui-input-block{margin-left:130px;}
        .layui-disabled, .layui-disabled:hover{color: #797979 !important;}
        .layui-treeSelect .ztree li span.button.root_close{
            background-image: url(/css/zTreeStyle/img/zTreeStandard.png)!important;
            background-position: -74px -54px!important;
        }
        .layui-treeSelect .ztree li span.button.root_open{
            background-image: url(/css/zTreeStyle/img/zTreeStandard.png)!important;
            background-position: -92px -54px!important;
        }
        .step-bar{
            z-index: 0;
        }
        .big-question{
            border: 1px solid #DEDEDE;
            border-radius: 4px;
            padding:15px 15px;
            margin-bottom:10px;
        }
        .big-question h3{
            display: inline-block;
        }
        .layui-input-block{

            margin-left:40px;
        }
        #dropdown{
            border:1px solid #E6E6E6;
            border-radius: 3px;
            margin-top: 3px;
            width: 109px;
            display: none;
        }
        #dropdown li{
            line-height: 30px;
            padding-left: 5px;

        }
    </style>
    <link rel="stylesheet" href="/ui/css/zTreeStyle/zTreeStyle.css"/>
    <link rel="stylesheet" href="/lib/zTree_v3/css/zTreeStyle/zTreeStyle.css"/>
    <script type="text/javascript" src="/lib/zTree_v3/js/jquery.ztree.all.min.js"></script>


<body>
<div>
    <!--
 自定义布局表单控件
 -->
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body" style="padding: 15px;">
                <div id="step"  class="step-body">
                    <div class="step-header">
                        <ul>
                            <li>
                                <span class="step-name">基础信息</span>
                            </li>
                            <li>
                                <span class="step-name">添加试题</span>
                            </li>
                        </ul>
                    </div>
                    <div class="step-content" style="margin-top: 50px;">
                        <div class="step-list">
                            <form class="layui-form form" action="" lay-filter="qaForm">
                                <div class="layui-form-item">
                                    <label class="layui-form-label">试卷名称<span style="color:red">*</span></label>
                                    <div class="layui-input-block" style="width: 80%;float:left;">
                                        <input type="text" name="pName" placeholder="请输入试卷名称" autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">所属目录<span style="color: red">*</span></label>
                                    <div class="layui-input-block" style="width: 80%;float:left;">
                                        <input type="text" name="" id="testId" lay-filter="tree" required  lay-verify="required" placeholder="请选择所属目录" autocomplete="off" class="layui-input">
                                        <input type="hidden" name="testId"  class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">培训中心<span style="color: red">*</span></label>
                                    <div class="layui-input-block" style="width: 80%;float:left;">
                                        <input type="text"  id="trainCenter" name="trainCenter" style="float: left;width: 85%;"  deptid="" deptName="" required  lay-verify="required" placeholder="请选择培训中心" autocomplete="off" class="layui-input">
                                        <div class="layui-form-mid layui-word-aux"><a href="javascript:;" style="color: #207BD6;padding:0px 10px" id="add">添加</a><a href="javascript:;" id="clear" style="color: #207BD6;">清除</a></div>
                                        <%--                                            <input type="hidden"  class="layui-input">--%>
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">所属课程<span style="color: red">*</span></label>
                                    <div class="layui-input-block" style="width: 80%;float:left;">
                                        <input type="text" name="" id="course" lay-filter="tree" required  lay-verify="required" placeholder="请选择所属课程" autocomplete="off" class="layui-input">
                                        <input type="hidden" name="course" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">组卷方式<span style="color: red">*</span></label>
                                    <div class="layui-input-block" style="width: 80%;float:left;">
                                        <select name="testFormat" lay-filter="aihao">
                                            <option value="">请选择</option>
                                            <option value="0">固定组卷</option>
                                            <option value="1">随机组卷</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">试卷完成时间<span style="color: red">*</span></label>
                                    <div class="layui-input-block" style="width: 80%;float:left;">
                                        <input type="text" name="testTime"  style="float: left;width: 93%;" required  lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
                                        <div class="layui-form-mid layui-word-aux">分钟</div>
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">试卷考试截止时间</label>
                                    <div class="layui-input-block" style="width: 80%;float:left;">
                                        <input type="text" name="endTime" class="layui-input" id="endTime">
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">及格分数</label>
                                    <div class="layui-input-block" style="width: 80%;float:left;">
                                        <input type="text" name="passingScore" class="layui-input" id="passingScore">
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">补考次数</label>
                                    <div class="layui-input-block" style="width: 80%;float:left;">
                                        <input type="text" name="resitCount" class="layui-input" id="resitCount">
                                    </div>
                                </div>
                                <div class="layui-form-item" style="text-align: center">
                                    <div class="layui-input-block" style="float: none;">
                                        <button type="button" class="layui-btn save"  lay-filter="form-qaForm">保存</button>
                                        <button type="button" class="layui-btn cancleBtn"   >取消</button>
                                    </div>
                                </div>


                            </form>
                        </div>
                        <div class="step-list">
                            <div class="layui-row layui-col-space15">
                                <div class="layui-card">
                                    <div class="layui-lf" style="width:250px;float:left;margin-top: 20px;">
                                        <div class="testLeft">

                                        </div>
                                        <div class="big-question">
                                            <p>总题数：<span class="totalTopic">0</span>题</p>
                                            <p>当前总分：<span class="totalCount">0</span>分</p>
                                        </div>
                                    </div>
                                    <div class="layui-rt" style="width:calc(100% - 310px);float:left;margin-top: 20px;    margin-left: 40px;">
                                        <form class="layui-form rtForm" action="" lay-filter="rtForm">
                                            <div class="layui-form-item">
                                                <div class="layui-input-block" style="margin-left: 0px;">
                                                    <input type="text" style="text-align: center" name="title" required  lay-verify="required"  autocomplete="off" class="layui-input layui-disabled title" disabled="disabled">
                                                </div>
                                            </div>
                                            <div class="testCon">
                                                <div class="testList">

                                                </div>
                                            </div>

                                            <div class="layui-btn-dropdown">
                                                <button type="button" class="layui-btn dtoggle" dataType="close" >添加试题 <span class="layui-icon" style="font-size: 14px"></span></button>
                                                <ul  id="dropdown" style="">
                                                    <li><a href="#" class="addChoose" type="1">单选题</a></li>
                                                    <li><a href="#" class="addChoose" type="2">多选题</a></li>
                                                    <li><a href="#" class="addChoose" type="3">判断题</a></li>
                                                    <li><a href="#" class="addChoose" type="4">填空题</a></li>
                                                    <li><a href="#" class="addChoose" type="5">问答题</a></li>
                                                </ul>
                                            </div>
                                            <div class="layui-input-block" style="text-align: center;margin-left: 0px;">
                                                <button type="button" class="layui-btn preview"  lay-filter="form-qaForm">预览</button>
                                                <button type="button" class="layui-btn testSave"   >保存</button>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>

                        </div>
                    </div>
                </div>

            </div>
        </div>
    </div>
    <script type="text/javascript">
        // var type = $.GetRequest().type;
        // var sortId = $.GetRequest().sortId;
        var id = $.GetRequest().id;
        var title=$.GetRequest().title;
        var arr=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R'];
        var zmIndex=0;
        var zujuan=0;
        var testTypeArr=['','单选题','多选题','判断题','填空题','问答题']
        var eqTypeArr=['','简单','中等','困难']
        var course = '';
        var testTree;
        var courseTree;
        layui.use(['form','layer','treeSelect','steps','laydate'],function(){
            var $ = layui.jquery,
                form = layui.form,
                layer = layui.layer,
                treeSelect = layui.treeSelect;
            var laydate = layui.laydate;
            var $step= $("#step").step();
            form.render();
            $("#add").on("click",function(){
                dept_id = "trainCenter";
                $.popWindow("/common/selectDept");
            });
            $('#clear').click(function () {
                $("#trainCenter").val("");
                $("#trainCenter").attr('deptName','');
                $("#trainCenter").attr('deptid','');
            });
            laydate.render({
                elem: '#endTime' //指定元素
                ,type: 'datetime'
            });

            //下拉樹獲取
            function treeSelects(treeId,url,intText,id,element){
                element =treeSelect.render({
                    elem: treeId,     // 选择器
                    data: url,  // 数据
                    type: 'get',      // 异步加载方式：get/post，默认get
                    placeholder: '请选择',   // 占位符
                    search: true,  // 是否开启搜索功能：true/false，默认false
                    style: {   // 一些可定制的样式
                        folder: {enable: true},
                        line: {enable: true}
                    },
                    click: function(d){     // 点击回调
                        console.log(d)
                        $('input[name="'+intText+'"]').val(d.current.id);

                        if(intText == 'course'){
                            if(d.current.courseStage!=''&&d.current.courseStage!=undefined){
                                courseType = 'course_stage'
                            }else{
                                courseType = 'course_id'
                            }
                        }
                        form.render()
                    },
                    // 加载完成后的回调函数
                    success: function (d) {
                        if(id!=''&&id!=undefined){
                            var treeIds = $('.layui-treeSelect-body').attr('id')
                            treeSelect.checkNode(treeId,treeIds,id);
                        }
                        form.render()
                    }
                });
            }
            if(id==''||id==undefined){
                treeSelects('#testId','/examTest/selectAllTree?testId=0','testId')
                // treeSelects('#trainCenter','/exaExamination/selectAllTree?deptId=0','trainCenter')
                treeSelects('#course','/trCourseStage/selCourseTree','course')
            }


            //点击添加试题
            $('.dtoggle').click(function(){
                var dataType = $(this).attr('dataType');
                if(dataType == 'close'){
                    $('#dropdown').show()
                    $('.dtoggle').attr('dataType','open');
                }else{
                    $('#dropdown').hide()
                    $('.dtoggle').attr('dataType','close');
                }

            })
            //删除左侧试题
            $(document).on('click','.layui-icon-delete',function(e){
                e.stopPropagation();

                $('.dtoggle').attr('dataType','close');
                var type = $(this).parents('.big-question').attr('type');
                $('.testList').find('.testType'+type+'').remove()
                $('.testList').find('.testList'+type+'').remove()
                $(this).parents('.big-question').remove();
                var num = 0
                for(var m=0;m<$('.topic').length;m++){
                    num += Number($('.topic').eq(m).text())
                }
                $('.totalTopic').text(num)
                countTest(type)
            })
            //删除右侧试题
            $(document).on('click','.delTest',function(){
                var type = $(this).attr('dataType')
                $(this).parents('.testCart').remove();
                var len = $('.testList'+type+'').find('.testCart').length;
                $('.topic'+type+'').text(len)
                var num = 0
                for(var m=0;m<$('.topic').length;m++){
                    num += Number($('.topic').eq(m).text())
                }
                $('.totalTopic').text(num)
                countTest(type)
            })
            //上移
            $(document).on('click','.prev',function(){
                $(this).parents('.testCart').prev().before($(this).parents('.testCart'))
            })
            //下移
            $(document).on('click','.next',function(){
                $(this).parents('.testCart').next().after($(this).parents('.testCart'))
            })

            //添加试题类型你
            $('.addChoose').click(function(){
                var type = $(this).attr('type');
                var text = $(this).text();
                $('#dropdown').hide();
                $('.dtoggle').attr('dataType','close');
                if($('.testList').find('.layui-form-item').hasClass('testType'+type+'')){
                    layer.msg('已添加'+text,{icon:6});
                    return false;
                }

                var str=' <div class="layui-form-item testType'+type+'" style="width:100%">\n' +
                    '                                                <div class="layui-input-inline" style="margin-left: 0px;width:100%">\n' +
                    '                                                    <input type="text" dataType="'+type+'" style="text-align: left;width:70%;float: left;margin-right:20px;" value="'+text+'" required  lay-verify="required"  autocomplete="off" class="layui-input">\n' +
                    '                                                    <span style="padding-top: 8px;display: inline-block;float: left">每题</span><input type="text" class="layui-input countType count'+type+'" dataType="'+type+'"  style="text-align: left;width:58px;float: left;" value="0" required  lay-verify="required"  autocomplete="off" ><span style="padding-top: 8px;display: inline-block;">分</span>\n' +
                    '                                                </div>\n' +    function(){
                        if(zujuan==0){
                            return  '                                                <div class="layui-input-inline" style="margin-left: 0px;">\n' +
                                '<button type="button" class="layui-btn addTest" style="margin-top: 10px"  dataType="'+type+'"><i class="layui-icon">&#xe654;</i>选择试题</button>'+
                                '                                                </div>'
                        }else{
                            return  '                                                <div class="layui-input-inline" style="margin-left: 0px;">\n' +
                                '<button type="button" class="layui-btn chooseTest" style="margin-top: 10px" dataType="'+type+'"><i class="layui-icon">&#xe654;</i>抽题规则</button>'+
                                '                                                </div>'
                        }
                    }()+
                    '                                            </div>'+
                    '                                                <div class="layui-input-inline testDraw testList'+type+'" style="margin-bottom: 10px;width:100%"></div>';
                var str2='  <div class="big-question testTitle'+type+'" type="'+type+'">\n' +
                    '                                                <p>'+text+' <i style="float:right;cursor:pointer;" class="layui-icon layui-icon-delete"></i> </p>\n' +
                    '                                                <p>共：<span class="topic topic'+type+'">0</span>题</p>\n' +
                    '                                                <p>共计：<span class="topicCount topicCount'+type+'">0</span>分</p>\n' +
                    '                                            </div>'
                $('.testList').append(str)
                $('.testLeft').append(str2)

            })

            //选择抽题规则
            $(document).on('click','.chooseTest',function(){
                var type = $(this).attr('dataType')
                var url = '/tbExamList/drawQuestion?type=add'
                drawQuestion(type,url,'add')
            })

            //编辑抽题规则
            $(document).on('click','.editDraw',function(){
                var type = $(this).attr('dataType');
                var num = $(this).attr('num')
                var eqType = $(this).attr('eqType')
                var testId = $(this).attr('testId')
                var testName = encodeURI($(this).attr('testName'))
                var url = '/tbExamList/drawQuestion?type=edit&num='+num+'&eqType='+eqType+'&testId='+testId+'&testName='+testName
                drawQuestion(type,url,'edit',$(this))
            })

            //删除抽题
            $(document).on('click','.delDraw',function(){
                var type= $(this).attr('dataType')
                $(this).parents('.testCart').remove();
                var len = $('.testList'+type+'').find('.testCart').length;
                var countTi = 0;
                var countNum=0;
                for(var i=0;i<len;i++){
                    countTi +=Number($('.testList'+type+'').find('.testCart').eq(i).find('.drawNum').text())
                    countNum += Number($('.testList'+type+'').find('.testCart').eq(i).find('.drawNum').text())*Number($('.count'+type+'').val())
                }
                $('.topic'+type+'').text(countTi)
                $('.topicCount'+type+'').text(countNum)
                drawTest(type)
            })


            function drawQuestion(type,url,dataType,e){
                var type = type
                layer.open({
                    type: 2,
                    title: '抽题规则',
                    shadeClose: true,
                    shade: 0.5,
                    maxmin: true, //开启最大化最小化按钮
                    area: ['500px', '300px'],
                    btn:['确定','取消'],
                    content:url,
                    yes:function(index){
                        var body = layer.getChildFrame('body', index);
                        var testId = body.find('[name="testId"]').val();
                        var testName = body.find('[name="testName"]').val();
                        var eqType = body.find('[name="eqType"]').val();
                        var num = body.find('[name="num"]').val();
                        $.ajax({
                            url:'/exaQuestions/getCountByTypeStatusSortId',
                            data:{sortId:testId,eqStatus:type,eqType:eqType},
                            dataType:'json',
                            success:function(res){
                                if(num>res){
                                    layer.msg('输入的试题数量超出题库该类型总数',{icon:2});
                                    return false;
                                }else{
                                    var str= '<div class="layui-card testCart" testId="'+testId+'" testName="'+testName+'" eqType="'+eqType+'" num="'+num+'" dataType="'+type+'"  style="background: #eee;">\n' +
                                        '                                                        <div class="layui-card-body header" style="height: 0px;"></div>\n' +
                                        '                                                        <div class="layui-card-body">\n' +
                                        '                                                             <p>所属目录：<span>'+testName+'</span></p>'+
                                        '                                                             <p>题型：<span>'+testTypeArr[type]+'</span></p>'+
                                        '                                                             <p>难度：<span>'+eqTypeArr[eqType]+'</span></p>'+
                                        '                                                             <p>抽题数量：<span class="drawNum" type="'+type+'">'+num+'</span>道</p>'+
                                        '                                                        </div>\n' +
                                        '                                                        <div class="layui-card-body" >\n' +
                                        '                                                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary editDraw" testId="'+testId+'" testName="'+testName+'" eqType="'+eqType+'" num="'+num+'" dataType="'+type+'" style="margin-left: 0px; margin-top: 10px;"><i class="layui-icon">&#xe642;</i> 编辑</button>\n' +
                                        '                                                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary delDraw" dataType="'+type+'" style="margin-left: 0px; margin-top: 10px;"><i class="layui-icon">&#xe640;</i> 删除</button>\n' +
                                        '                                                        </div>\n' +
                                        '                                                    </div>'
                                    if(dataType=='add'){
                                        $('.testList'+type+'').append(str);

                                    }else{
                                        e.parents('.testCart').after(str)
                                        e.parents('.testCart').remove();
                                    }

                                    var len = $('.testList'+type+'').find('.testCart').length;
                                    var countTi = 0;
                                    var countNum=0;
                                    for(var i=0;i<len;i++){
                                        countTi +=Number($('.testList'+type+'').find('.testCart').eq(i).find('.drawNum').text())
                                        countNum += Number($('.testList'+type+'').find('.testCart').eq(i).find('.drawNum').text())*Number($('.count'+type+'').val())
                                    }
                                    $('.topic'+type+'').text(countTi)
                                    $('.topicCount'+type+'').text(countNum)
                                    drawTest(type)
                                    layer.close(index)
                                }
                            }
                        })

                    }
                });
            }


            //点击选择试题
            $(document).on('click','.addTest',function(){
                var type= $(this).attr('dataType');
                layer.open({
                    type: 2,
                    title: '添加试题',
                    shadeClose: true,
                    shade: 0.5,
                    maxmin: true, //开启最大化最小化按钮
                    area: ['90%', '90%'],
                    btn:['确定','取消'],
                    content:'/tbExamList/index?type=addTest&dataType='+type,
                    yes:function(index){
                        var w = $('.layui-layer-iframe').find("iframe")[0].contentWindow;
                        var testArr = w.testArr;
                        var count = $('.count'+type+'').val();
                        if(testArr!=''){
                            var testStr=''
                            for(var i=0;i<testArr.length;i++){
                                var options = testArr[i].option;
                                testStr += '<div class="layui-card testCart" eqId="'+testArr[i].eqId+'" style="background: #eee;position:relative;min-height: 180px">\n' +
                                    '                                                        <div class="layui-card-body header" style="height: 21px;">\n' +
                                    '                                                            <div class="layui-input-inline" style="width: calc(100% - 150px);float: left;">'+testArr[i].eqSubject+'</div>\n' +
                                    '                                                            <div class="layui-input-inline" style="width: 150px;float: right;">\n' +
                                    '                                                                <input type="text" style="width: 58px;float: left;" value="'+count+'" dataType="'+type+'"  name="title" required  lay-verify="required"  autocomplete="off" class="layui-input gradeType grade'+type+'">\n' +
                                    '                                                                <div class="layui-form-mid layui-word-aux">分</div>\n' +
                                    '                                                            </div>\n' +
                                    '                                                        </div>\n' +
                                    '                                                        <div class="layui-card-body">\n' +function(){
                                        var str='';
                                        if(options!=''){
                                            var arr = options.split(';');
                                            if(type==1||type==2){
                                                for(var i=0;i<arr.length;i++){
                                                    str += '<p style="width: calc(100% - 150px);">'+arr[i]+'</p>'
                                                }
                                            }else{
                                                str=''
                                            }
                                        }else{
                                            if(type==3){
                                                str = '<p style="width: calc(100% - 150px);">对</p><p style="width: calc(100% - 150px);">错</p>'
                                            }else{
                                                str=''
                                            }
                                        }
                                        return str;
                                    }()+
                                    '                                                        </div>\n' +
                                    '                                                        <div class="layui-card-body" >\n' +
                                    '                                                            <p style="width: calc(100% - 150px);">答案：<span>'+function(){
                                        if(type==3){
                                            if(testArr[i].answer==0){
                                                return '对'
                                            }else{
                                                return '错'
                                            }
                                        }else{
                                            return testArr[i].answer
                                        }
                                    }()+'</span></p>\n' +
                                    '                                                            <p style="width: calc(100% - 150px);">解析：<span>'+testArr[i].analysis+'</span></p>\n' +
                                    '                                                        </div>\n' +
                                    '                                                        <div style="position: absolute;width: 90px;right: 75px;top:50px;">\n' +
                                    '                                                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary delTest" dataType="'+type+'" style="margin-left: 0px; margin-top: 10px;"><i class="layui-icon">&#xe640;</i> 删除</button>\n' +
                                    '                                                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary prev" style="margin-left: 0px; margin-top: 10px;"><i class="layui-icon">&#xe619;</i> 上移</button>\n' +
                                    '                                                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary next" style="margin-left: 0px; margin-top: 10px;"><i class="layui-icon">&#xe61a;</i> 下移</button>\n' +
                                    '                                                        </div>\n' +
                                    '                                                    </div>'
                            }
                            $('.testList'+type+'').append(testStr)
                            var len = $('.testList'+type+'').find('.testCart').length;
                            $('.topic'+type+'').text(len)
                            var num = 0;
                            var countNum = 0;
                            for(var m=0;m<$('.topic').length;m++){
                                num += Number($('.topic').eq(m).text())
                            }
                            countTest(type)
                            $('.totalTopic').text(num)
                            layer.close(index)
                        }
                    },
                    success:function(index){


                    }
                });
            })

            //获取详情
            if(id!=''&&id!='undefined'){
                dataId = id;
                $step.goStep(2);//到指定步
                $.ajax({
                    url:'/exaPaper/selectById',
                    data:{pId:id},
                    type:'get',
                    dataType:'json',
                    success:function(res){
                        var data = res.object;
                        if(res.code == '0'){
                            $('[name="title"]').val(data.pName);
                            $('[name="pName"]').val(data.pName);
                            $('[name="testTime"]').val(data.testTime);
                            $('[name="endTime"]').val(data.endTime);
                            $('[name="passingScore"]').val(data.passingScore);
                            $('[name="resitCount"]').val(data.resitCount);
                            $('[name="testFormat"]').val(data.testFormat)
                            $('#trainCenter').attr('deptid',data.trainCenter)
                            $('#trainCenter').attr('deptname',data.trainCenterName)
                            $('#trainCenter').val(data.trainCenterName)
                            treeSelects('#testId','/examTest/selectAllTree?testId=0','testId',data.testId)
                            // treeSelects('#trainCenter','/exaExamination/selectAllTree?deptId=0','trainCenter')
                            if(data.courseId!=undefined&&data.courseId>0){
                                treeSelects('#course','/trCourseStage/selCourseTree','course',data.courseId)
                                $('[name="course"]').val(data.courseId)
                            }else if(data.courseStage!=undefined&&data.courseStage>0){
                                treeSelects('#course','/trCourseStage/selCourseTree','course',data.courseStage)
                                $('[name="course"]').val(data.courseStage)
                            }else {
                                treeSelects('#course','/trCourseStage/selCourseTree','course')
                            }
                            $('[name="testId"]').val(data.testId)
                            form.render('select')
                            var str='';
                            var str2 = ''
                            if(data.testFormat == 0){
                                zujuan = 0;
                                var len = 5;
                                for(var i=1;i<=len;i++){
                                    var list = 'list'+i;
                                    var type = i;
                                    if(data[list].length>0){
                                        str +=' <div class="layui-form-item testType'+type+'" style="width:100%">\n' +
                                            '                                                <div class="layui-input-inline" style="margin-left: 0px;width:100%">\n' +
                                            '                                                    <input type="text" dataType="'+type+'" style="text-align: left;width:70%;float: left;margin-right:20px;" value="'+testTypeArr[type]+'" required  lay-verify="required"  autocomplete="off" class="layui-input">\n' +
                                            '                                                    <span style="padding-top: 8px;display: inline-block;float: left">每题</span><input type="text" class="layui-input countType count'+i+'" dataType="'+type+'"  style="text-align: left;width:58px;float: left;" value="'+data[list][0].score+'" required  lay-verify="required"  autocomplete="off" ><span style="padding-top: 8px;display: inline-block;">分</span>\n' +
                                            '                                                </div>' +
                                            '                                              <div class="layui-input-inline" style="margin-left: 0px;">\n' +
                                            '<button type="button" class="layui-btn addTest" style="margin-top: 10px"  dataType="'+type+'"><i class="layui-icon">&#xe654;</i>选择试题</button>'+
                                            '                                                </div>'+
                                            '                                            </div>'+
                                            '                                                <div class="layui-input-inline testDraw testList'+type+'" style="margin-bottom: 10px;width:100%">'+function(){
                                                var testStr=''
                                                for(var j=0;j<data[list].length;j++){
                                                    testStr += '<div class="layui-card testCart" eqId="'+data[list][j].eqId+'" style="background: #eee;position:relative;min-height: 180px">\n' +
                                                        '                                                        <div class="layui-card-body header" style="height: 21px;">\n' +
                                                        '                                                            <div class="layui-input-inline" style="width: calc(100% - 150px);float: left;">'+data[list][j].eqSubject+'</div>\n' +
                                                        '                                                            <div class="layui-input-inline" style="width: 150px;float: right;">\n' +
                                                        '                                                                <input type="text" style="width: 58px;float: left;" value="'+data[list][j].score+'" dataType="'+type+'"  name="title" required  lay-verify="required"  autocomplete="off" class="layui-input gradeType grade'+type+'">\n' +
                                                        '                                                                <div class="layui-form-mid layui-word-aux">分</div>\n' +
                                                        '                                                            </div>\n' +
                                                        '                                                        </div>\n' +
                                                        '                                                        <div class="layui-card-body">\n' +function(){
                                                            var str='';
                                                            if(data[list][j].option!=''){
                                                                var arr = data[list][j].option.split(';');
                                                                if(type==1||type==2){
                                                                    for(var i=0;i<arr.length;i++){
                                                                        str += '<p style="width: calc(100% - 150px);">'+arr[i]+'</p>'
                                                                    }
                                                                }else{
                                                                    str=''
                                                                }
                                                            }else{
                                                                if(type==3){
                                                                    str = '<p style="width: calc(100% - 150px);">对</p><p style="width: calc(100% - 150px);">错</p>'
                                                                }else{
                                                                    str=''
                                                                }
                                                            }
                                                            return str;
                                                        }()+
                                                        '                                                        </div>\n' +
                                                        '                                                        <div class="layui-card-body" >\n' +
                                                        '                                                            <p style="width: calc(100% - 150px);">答案：<span>'+function(){
                                                            if(type==3){
                                                                if(data[list][j].answer==0){
                                                                    return '对'
                                                                }else{
                                                                    return '错'
                                                                }
                                                            }else{
                                                                return data[list][j].answer
                                                            }
                                                        }()+'</span></p>\n' +
                                                        '                                                            <p style="width: calc(100% - 150px);">解析：<span>'+data[list][j].analysis+'</span></p>\n' +
                                                        '                                                        </div>\n' +
                                                        '                                                        <div style="position: absolute;width: 90px;right: 75px;top:50px;">\n' +
                                                        '                                                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary delTest" dataType="'+type+'" style="margin-left: 0px; margin-top: 10px;"><i class="layui-icon">&#xe640;</i> 删除</button>\n' +
                                                        '                                                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary prev" style="margin-left: 0px; margin-top: 10px;"><i class="layui-icon">&#xe619;</i> 上移</button>\n' +
                                                        '                                                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary next" style="margin-left: 0px; margin-top: 10px;"><i class="layui-icon">&#xe61a;</i> 下移</button>\n' +
                                                        '                                                        </div>\n' +
                                                        '                                                    </div>'
                                                }
                                                return testStr;
                                            }()+'</div>';
                                        str2 +='  <div class="big-question testTitle'+type+'" type="'+type+'">\n' +
                                            '                                                <p>'+testTypeArr[type]+' <i style="float:right;cursor:pointer;" class="layui-icon layui-icon-delete"></i> </p>\n' +
                                            '                                                <p>共：<span class="topic topic'+type+'">0</span>题</p>\n' +
                                            '                                                <p>共计：<span class="topicCount topicCount'+type+'">0</span>分</p>\n' +
                                            '                                            </div>'
                                    }
                                }
                                $('.testList').append(str)
                                $('.testLeft').append(str2)
                                for(var m=1;m<=len;m++){
                                    var length = $('.testList'+m+'').find('.testCart').length;
                                    $('.topic'+m+'').text(length)
                                    var num = 0;
                                    var countNum = 0;
                                    for(var n=0;n<$('.topic').length;n++){
                                        num += Number($('.topic').eq(n).text())
                                    }
                                    countTest(m)
                                    $('.totalTopic').text(num)
                                }

                            }else{
                                zujuan = 1;
                                if(data.list6.length>0){
                                    data.list1=[];
                                    data.list2 = [];
                                    data.list3=[];
                                    data.list4=[];
                                    data.list5=[];
                                    for(var a=0;a<data.list6.length;a++){
                                        if(data.list6[a].eqStatus ==1){
                                            data.list1.push(data.list6[a])
                                        }else if(data.list6[a].eqStatus ==2){
                                            data.list2.push(data.list6[a])
                                        }else if(data.list6[a].eqStatus ==3){
                                            data.list3.push(data.list6[a])
                                        }else if(data.list6[a].eqStatus ==4){
                                            data.list4.push(data.list6[a])
                                        }else if(data.list6[a].eqStatus ==5){
                                            data.list5.push(data.list6[a])
                                        }
                                    }
                                    var len = 5;
                                    for(var i=1;i<=len;i++){
                                        var list = 'list'+i;
                                        var type = i;
                                        if(data[list].length>0){
                                            str +=' <div class="layui-form-item testType'+type+'" style="width:100%">\n' +
                                                '                                                <div class="layui-input-inline" style="margin-left: 0px;width:100%">\n' +
                                                '                                                    <input type="text" dataType="'+type+'" style="text-align: left;width:70%;float: left;margin-right:20px;" value="'+testTypeArr[type]+'" required  lay-verify="required"  autocomplete="off" class="layui-input">\n' +
                                                '                                                    <span style="padding-top: 8px;display: inline-block;float: left">每题</span><input type="text" class="layui-input countType count'+type+'" dataType="'+type+'"  style="text-align: left;width:58px;float: left;" value="'+data[list][0].score+'" required  lay-verify="required"  autocomplete="off" ><span style="padding-top: 8px;display: inline-block;">分</span>\n' +
                                                '                                                </div>\n' +
                                                '                                                <div class="layui-input-inline" style="margin-left: 0px;">\n' +
                                                '<button type="button" class="layui-btn chooseTest" style="margin-top: 10px" dataType="'+type+'"><i class="layui-icon">&#xe654;</i>抽题规则</button>'+
                                                '                                                </div>'+
                                                '                                            </div>'+
                                                '                                                <div class="layui-input-inline testDraw testList'+type+'" style="margin-bottom: 10px;width:100%">'+function(){
                                                    var testStr = '';
                                                    for(var n=0;n<data[list].length;n++){
                                                        testStr += '<div class="layui-card testCart" testId="'+data[list][n].sortId+'" testName="'+data[list][n].sortName+'" eqType="'+data[list][n].eqType+'" num="'+data[list][n].num+'" dataType="'+type+'"  style="background: #eee;">\n' +
                                                            '                                                        <div class="layui-card-body header" style="height: 0px;"></div>\n' +
                                                            '                                                        <div class="layui-card-body">\n' +
                                                            '                                                             <p>所属目录：<span>'+data[list][n].sortName+'</span></p>'+
                                                            '                                                             <p>题型：<span>'+testTypeArr[type]+'</span></p>'+
                                                            '                                                             <p>难度：<span>'+eqTypeArr[data[list][n].eqType]+'</span></p>'+
                                                            '                                                             <p>抽题数量：<span class="drawNum" type="'+type+'">'+data[list][n].num+'</span>道</p>'+
                                                            '                                                        </div>\n' +
                                                            '                                                        <div class="layui-card-body" >\n' +
                                                            '                                                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary editDraw" testId="'+data[list][n].sortId+'" testName="'+data[list][n].sortName+'" eqType="'+data[list][n].eqType+'" num="'+data[list][n].num+'" dataType="'+type+'" style="margin-left: 0px; margin-top: 10px;"><i class="layui-icon">&#xe642;</i> 编辑</button>\n' +
                                                            '                                                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary delDraw" dataType="'+type+'" style="margin-left: 0px; margin-top: 10px;"><i class="layui-icon">&#xe640;</i> 删除</button>\n' +
                                                            '                                                        </div>\n' +
                                                            '                                                    </div>'
                                                    }
                                                    return testStr;
                                                }()+'</div>';
                                            str2 +='  <div class="big-question testTitle'+type+'" type="'+type+'">\n' +
                                                '                                                <p>'+testTypeArr[type]+' <i style="float:right;cursor:pointer;" class="layui-icon layui-icon-delete"></i> </p>\n' +
                                                '                                                <p>共：<span class="topic topic'+type+'">0</span>题</p>\n' +
                                                '                                                <p>共计：<span class="topicCount topicCount'+type+'">0</span>分</p>\n' +
                                                '                                            </div>'
                                        }
                                    }
                                    $('.testList').append(str)
                                    $('.testLeft').append(str2)
                                    for(var m=1;m<=len;m++){
                                        var length = $('.testList'+m+'').find('.testCart').length;
                                        var countTi = 0;
                                        var countNum=0;
                                        for(var i=0;i<length;i++){
                                            countTi +=Number($('.testList'+m+'').find('.testCart').eq(i).find('.drawNum').text())
                                            countNum += Number($('.testList'+m+'').find('.testCart').eq(i).find('.drawNum').text())*Number($('.count'+m+'').val())
                                        }
                                        $('.topic'+m+'').text(countTi)
                                        $('.topicCount'+m+'').text(countNum)
                                        drawTest(m)
                                    }


                                }
                            }
                        }

                        form.render();
                    }
                })
            }

            //监听提交
            $('.save').click(function(){
                if(check()){
                    var pName = $('input[name="pName"]').val();
                    var testFormat = $('[name="testFormat"]').val();
                    zujuan = testFormat;
                    $('.title').val(pName);
                    var data = {
                        pName:$('[name="pName"]').val(),
                        testId:$('[name="testId"]').val(),
                        trainCenter:$('#trainCenter').attr('deptId'),
                        testFormat:zujuan,
                        status:0,
                        pProblem:'',
                        endTime:$('[name="endTime"]').val(),
                        testTime:$('[name="testTime"]').val(),
                        resitCount:$('[name="resitCount"]').val(),
                        passingScore:$('[name="passingScore"]').val()
                    }
                    if($('[name="course"]').val()!=''||$('[name="course"]').val()!=undefined){
                        data.courseId = $('[name="course"]').val()
                    }else{
                        if(courseType == 'course_id'){
                            data.courseId = $('[name="course"]').val()
                        }else{
                            data.courseStage = $('[name="course"]').val()
                        }
                    }
                    var url=''
                    if(title=='0'){
                        url='/exaPaper/addPaper'
                    }else{
                        url='/exaPaper/updatePaper';
                        data.pId=id;
                    }
                    $.ajax({
                        url:url,
                        type:'post',
                        data:data,
                        dataType:'json',
                        success:function(res){
                            if(res.code == 0){
                                layer.msg('保存成功',{icon:1,time:2000})
                                dataId = res.object
                                $step.nextStep();
                            }
                        }
                    })

                }

            })
            //监听试题保存
            $('.testSave').click(function(){
                var data = {
                    pId:dataId,
                    countScores:Number($('.totalCount').text())
                }
                var pProblem=''
                if(zujuan==0){
                    for(var i=0;i<$('.testCart').length;i++){
                        var eqId = $('.testCart').eq(i).attr('eqId');
                        var grade = $('.testCart').eq(i).find('.gradeType').val();
                        pProblem += eqId+'|'+grade+','
                    }
                }else{
                    for(var i=0;i<$('.testCart').length;i++){
                        var type = $('.testCart').eq(i).attr('dataType');
                        var testId = $('.testCart').eq(i).attr('testId');
                        var eqType = $('.testCart').eq(i).attr('eqType');
                        var num = $('.testCart').eq(i).attr('num');
                        var course = $('.count'+type+'').val()
                        pProblem += type+'|'+testId+'|'+eqType+'|'+num+'|'+course+','
                    }
                }
                data.pProblem = pProblem;
                $.ajax({
                    url:'/exaPaper/updatePaper',
                    type:'post',
                    data:data,
                    dataType:'json',
                    success:function(res){
                        if(res.code == 0){
                            layer.msg('保存成功',{icon:1,time:2000})
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                            parent.location.reload();
                        }
                    }
                })
            })
            //点击预览
            $('.preview').click(function(){
                var data = {
                    pId:dataId,
                    countScores:Number($('.totalCount').text())
                }
                var pProblem=''
                if(zujuan==0){
                    for(var i=0;i<$('.testCart').length;i++){
                        var eqId = $('.testCart').eq(i).attr('eqId');
                        var grade = $('.testCart').eq(i).find('.gradeType').val();
                        pProblem += eqId+'|'+grade+','
                    }
                }else{
                    for(var i=0;i<$('.testCart').length;i++){
                        var type = $('.testCart').eq(i).attr('dataType');
                        var testId = $('.testCart').eq(i).attr('testId');
                        var eqType = $('.testCart').eq(i).attr('eqType');
                        var num = $('.testCart').eq(i).attr('num');
                        var course = $('.count'+type+'').val()
                        pProblem += type+'|'+testId+'|'+eqType+'|'+num+'|'+course+','
                    }
                }
                data.pProblem = pProblem;
                $.ajax({
                    url:'/exaPaper/updatePaper',
                    type:'post',
                    data:data,
                    dataType:'json',
                    success:function(res){
                        if(res.code == 0){
                            layer.open({
                                type: 2,
                                title: '预览',
                                shadeClose: true,
                                shade: 0.5,
                                maxmin: true, //开启最大化最小化按钮
                                area: ['893px', '90%'],
                                content:'/tbExamList/testPreview?id='+dataId,
                            });
                        }
                    }
                })
            })

            //监听取消
            $('.cancleBtn').click(function(){
                var index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index);
            })
            //    創建試卷必填
            function check(){
                if($('[name="pName"]').val()==''){
                    layer.msg('请填写试卷题目',{icon:2});
                    return false;
                }
                if($('[name="testId"]').val()==''){
                    layer.msg('请选择所属目录',{icon:2});
                    return false;
                }
                if($('[name="trainCenter"]').val()==''){
                    layer.msg('请选择培训中心',{icon:2});
                    return false;
                }
                if($('[name="course"]').val()==''){
                    layer.msg('请选择所属课程',{icon:2});
                    return false;
                }
                if($('[name="testFormat"]').val()==''){
                    layer.msg('请选择组卷方式',{icon:2});
                    return false;
                }
                if($('[name="testTime"]').val()==''){
                    layer.msg('请填写试卷完成时间',{icon:2});
                    return false;
                }
                return true;
            }
        });
        $(document).on('input propertychange','.countType',function(){
            var type = $(this).attr('dataType')
            if(zujuan ==0){
                var value = $(this).val();
                $('.grade'+type+'').val(value);
                countTest(type)
            }else{
                var len = $('.testList'+type+'').find('.testCart').length;
                var countTi = 0;
                var countNum=0;
                for(var i=0;i<len;i++){
                    countTi +=Number($('.testList'+type+'').find('.testCart').eq(i).find('.drawNum').text())
                    countNum += Number($('.testList'+type+'').find('.testCart').eq(i).find('.drawNum').text())*Number($('.count'+type+'').val())
                }
                $('.topic'+type+'').text(countTi)
                $('.topicCount'+type+'').text(countNum)
                drawTest(type)
            }

        })
        $(document).on('input propertychange','.gradeType',function(){
            var type = $(this).attr('dataType')
            var value = $(this).val();
            $('.count'+type+'').val(0)
            countTest(type)
        })
        function countTest(type){
            var countNum = 0
            for(var i=0;i<$('.grade'+type+'').length;i++){
                countNum += Number($('.grade'+type+'').eq(i).val())
            }
            $('.topicCount'+type+'').text(countNum)
            var num = 0;
            for(var j=0;j<$('.topicCount').length;j++){
                num += Number($('.topicCount').eq(j).text())
            }
            $('.totalCount').text(num)

        }
        function drawTest(type){
            var topic=0
            for(var m=0;m<$('.topic').length;m++){
                topic += Number($('.topic').eq(m).text())
            }
            $('.totalTopic').text(topic)
            var topicCount = 0;
            for(var q=0;q<$('.topicCount').length;q++){
                topicCount += Number($('.topicCount').eq(q).text())
            }
            $('.totalCount').text(topicCount)
        }



    </script>
</div>
<style>
    .layui-form-checkbox i{
        border-left: 1px solid #d2d2d2;
    }
    .active{
        display: block;
    }
    .nonactive{
        display: none;
    }
</style>
</body>
</html>
